#include "EventQtSlotConnect.h"
#include "ui_EventQtSlotConnect.h"

#include <vtkEventQtSlotConnect.h>
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkInteractorStyleTrackballActor.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkVersion.h>
#include <vtkRegularPolygonSource.h>
#include <vtkFollower.h>

#if VTK_VERSION_NUMBER >= 89000000000ULL
#define VTK890 1
#endif

// Constructor
EventQtSlotConnect::EventQtSlotConnect(QWidget *parent)
        : QMainWindow(parent), ui(new Ui::EventQtSlotConnect) {
    this->ui->setupUi(this);

    vtkNew<vtkNamedColors> colors;

    vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
#if VTK890
    this->ui->qvtkWidget->setRenderWindow(renderWindow);
#else
    this->ui->qvtkWidget->SetRenderWindow(renderWindow);
#endif

    vtkNew<vtkEventQtSlotConnect> slotConnector;
    this->Connections = slotConnector;

    // Sphere
    vtkNew<vtkSphereSource> sphereSource;
    sphereSource->Update();
    vtkNew<vtkPolyDataMapper> sphereMapper;
    sphereMapper->SetInputConnection(sphereSource->GetOutputPort());

    vtkNew<vtkActor> sphereActor;
    sphereActor->SetMapper(sphereMapper);
    sphereActor->GetProperty()->SetColor(colors->GetColor4d("Tomato").GetData());

    // VTK Renderer
    vtkNew<vtkRenderer> ren;
    ren->AddActor(sphereActor);
    ren->SetBackground(colors->GetColor3d("SteelBlue").GetData());
    this->renderer = ren;

    this->ui->qvtkWidget->renderWindow()->AddRenderer(this->renderer);


    vtkNew<vtkRegularPolygonSource> polygon;
    // Comment this line to generate a disk instead of a circle.
    polygon->GeneratePolygonOff();
    polygon->SetNumberOfSides(50);
    polygon->SetRadius(5);
    polygon->SetCenter(0, 0, 0);

    this->polygonSource = polygon;

    vtkNew<vtkPolyDataMapper> mapper;
    mapper->SetInputConnection(this->polygonSource->GetOutputPort());

    vtkNew<vtkFollower> follower;
    follower->SetMapper(mapper);
    follower->GetProperty()->SetColor(colors->GetColor3d("Cornsilk").GetData());
    this->renderer->AddActor(follower);
    follower->SetCamera(this->renderer->GetActiveCamera());


    this->Connections->Connect(
            this->ui->qvtkWidget->renderWindow()->GetInteractor(),
            vtkCommand::LeftButtonPressEvent, this,
            SLOT(slot_clicked(vtkObject * , unsigned long, void * , void * )));

    this->Connections->Connect(
            this->ui->qvtkWidget->renderWindow()->GetInteractor(),
            vtkCommand::MouseMoveEvent, this,
            SLOT(slot_mouseMove(vtkObject * , unsigned long, void * , void * )));

};

EventQtSlotConnect::~EventQtSlotConnect() {
    delete this->ui;
}

void EventQtSlotConnect::slot_clicked(vtkObject *, unsigned long, void *, void *) {
    std::cout << "Clicked." << std::endl;
}

void EventQtSlotConnect::slot_mouseMove(vtkObject *, unsigned long, void *, void *) {
    std::cout << "MouseMove." << std::endl;

    auto *interactor = this->ui->qvtkWidget->renderWindow()->GetInteractor();
    int *clickPos = interactor->GetEventPosition();
    std::cout << clickPos[0] << " " << clickPos[1] << " " << clickPos[2] << std::endl;


    this->renderer->Render();

}
